'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 玩家联盟BOSS信息
 * @author sundbcn@gmail.com (sun debing)
 */

var mongoose = require('mongoose');
var _ = require('lodash');
var dataApi = require('../libs/dataApi');
var constantUtil = require('../util/constantUtil.js');

var _initBossFightTimes = function () {
    return constantUtil.value('union_boss_challenge_times');
};

/**
 * 玩家联盟BOSS信息
 */
var roleUnionBossSchema = new mongoose.Schema({
    // 玩家id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 剩余次数
    remain_times: {
        type: Number,
        default: _initBossFightTimes
    },

    // 累计伤害
    total_damage: {
        type: Number,
        default: 0
    },

    // 已领取成员奖励列表
    picked_member_reward: {
        type: [Number],
        default: []
    },

    // 已领取累计伤害奖励列表
    picked_total_damage_reward: {
        type: [Number],
        default: []
    },
});

/**
 * 每日刷新
 * @return {[type]} [description]
 */
roleUnionBossSchema.methods.doDailyRefresh = function (reputation_num) {
    var init_times = _initBossFightTimes();
    if (this.remain_times < init_times) {
        this.remain_times = init_times;
    }
};

/**
 * 查询奖励领取状态
 * @return {[type]} [description]
 */
roleUnionBossSchema.methods.getRewardStatus = function (boss_id) {
    var self = this;
    var status = 1;
    _.forEach(self.picked_member_reward, function (item) {
        if (item === boss_id) {
            status = 0;
            return false;
        }
        return true;
    });
    return status;
};

/**
 * 减少挑战次数
 * @return {[type]} [description]
 */
roleUnionBossSchema.methods.decRemainTimes = function (boss_id) {
    if (this.remain_times >= 1) {
        this.remain_times--;
        return true;
    } else {
        return false;
    }
};

/**
 * 增加累计伤害值
 * @return {[type]} [description]
 */
roleUnionBossSchema.methods.addTotalDamage = function (damage) {
    if (damage < 1) {
        return false;
    }

    this.total_damage += damage;
    return true;
};

/**
 * 加入到已领取成员奖励列表
 * @return {[type]} [description]
 */
roleUnionBossSchema.methods.addToMemberRewardList = function (boss_id) {
    var self = this;
    var exists = false;
    _.forEach(self.picked_member_reward, function (item) {
        if (item === boss_id) {
            exists = true;
            return false;
        }
    });
    if (exists) {
        return false;
    }
    self.picked_member_reward.push(boss_id);

    return true;
};

/**
 * 加入到已领取累计伤害奖励列表
 * @return {[type]} [description]
 */
roleUnionBossSchema.methods.addToTotalDamageRewardList = function (total_damage) {
    var self = this;
    var exists = false;
    _.forEach(self.picked_total_damage_reward, function (item) {
        if (item === total_damage) {
            exists = true;
            return false;
        }
    });
    if (exists) {
        return false;
    }
    self.picked_total_damage_reward.push(total_damage);

    return true;
};

mongoose.model('RoleUnionBoss', roleUnionBossSchema);
